package com.ncmmall.api.apis.mall;

import com.ncmmall.api.apis.service.Result;
import com.ncmmall.domain.business.marketing.seckill.SeckillSession;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 秒杀活动
 *
 * @author linxs
 * @date 2020-02-13 21:55
 */
@RestController
@RequestMapping(value = "/seckill")
@Slf4j
public class SeckillApi {

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 秒杀场次
     */
    @RequestMapping(value = "/sessiones", method = RequestMethod.GET)
    @ResponseBody
    public Result sessiones(HttpServletRequest request) {
        Result result = new Result();
        try {
            List<SeckillSession> sessions = queryChannelService.findAll("select s from SeckillSession s where s.status = 2 order by s.seckillTime asc ", SeckillSession.class);
            return result.ok().inject(ImmutableMap.of("sessiones", sessions, "systemTime", System.currentTimeMillis()));
        } catch (IllegalArgumentException e) {
            return result.fail().setMsg(e.getMessage());
        } catch (Exception e) {
            log.error("[获取秒杀场次异常]", e);
            return result.fail().setMsg("获取数据异常");
        }

    }


}
